﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using EShop.Model;
using EShop.BLL;

using EShop.Common;

namespace EShop.Client
{
    public partial class frmMerchandiseOrderInputMemberID : frmBase
    {
        public frmMerchandiseOrderInputMemberID(MemberInfo oMemberInfo)
        {
            InitializeComponent();
            this.oMemberInfo = oMemberInfo;
        }
        private MemberInfo oMemberInfo;
        private int MemberIDLength = 4;


        private void txtMemberID_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == (char)13)
            {
                if (string.IsNullOrEmpty(txtMemberID.Text))
                {
                    this.DialogResult = DialogResult.Cancel;
                }
                else
                {
                    string strMemberID = SystemLibrary.SQLQuoteName(txtMemberID.Text);
                    while (strMemberID.Length < MemberIDLength)
                    {
                        strMemberID = "0" + strMemberID;
                    }

                    MemberManagerBLL oMemberManagerBLL = new MemberManagerBLL();
                    DataTable dtMemberUser = oMemberManagerBLL.GetMember(strMemberID, 0);
                    DataTable dtMemberSystem = oMemberManagerBLL.GetMember(strMemberID, 1);
                    int indexUser = dtMemberUser.Rows.Count;
                    int indexSystem = dtMemberSystem.Rows.Count;
                    if (indexUser > 0 || indexSystem > 0)
                    {
                        oMemberInfo.MemberID = strMemberID;
                        if (indexUser > 0) oMemberInfo.Discount = Convert.ToDecimal(dtMemberUser.Rows[0]["LevelDiscount"]);
                        else oMemberInfo.Discount = Convert.ToDecimal(dtMemberSystem.Rows[0]["LevelDiscount"]);
                        this.DialogResult = DialogResult.OK;
                    }
                    else
                    {
                        if (MessageBox.Show("无效的会员卡号,是否使用系统默认会员？", "会员卡号无效", MessageBoxButtons.YesNo, MessageBoxIcon.Asterisk, MessageBoxDefaultButton.Button2) == DialogResult.Yes)
                        {
                            this.DialogResult = DialogResult.Cancel;
                        }
                        else
                        {
                            this.txtMemberID.SelectAll();
                            this.txtMemberID.Focus();
                        }
                    }

                }
            }
            if (e.KeyChar == (char)27)
            {
                this.DialogResult = DialogResult.Retry;
                Close();
            }

        }
    }
}
